package com.roads.engineering.controller;

import java.util.List;

import com.roads.common.constant.Constants;
import com.roads.common.constant.UserConstants;
import com.roads.common.utils.SecurityUtils;
import com.roads.engineering.service.ISummaryService;
import com.roads.project.domain.ProPeriod;
import com.roads.project.service.IProBoqMeterageService;
import com.roads.project.service.IProPeriodService;
import com.roads.workflow.domain.WorkflowTaskDelivery;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.roads.common.annotation.Log;
import com.roads.common.core.controller.BaseController;
import com.roads.common.core.domain.AjaxResult;
import com.roads.common.enums.BusinessType;
import com.roads.engineering.domain.ChangeOrderMaster;
import com.roads.engineering.service.IChangeOrderMasterService;
import com.roads.common.core.page.TableDataInfo;

/**
 * 变更令申报Controller
 * 
 * @author nhp
 * @date 2022-01-07
 */
@RestController
@RequestMapping("/engineering/change")
public class ChangeOrderMasterController extends BaseController {

    @Autowired
    private IChangeOrderMasterService service;

    @Autowired
    private IProBoqMeterageService boqMeterageService;

    @Autowired
    private ISummaryService summaryService;

    @Autowired
    private IProPeriodService periodService;

    /**
     * 查询列表
     */
    @PreAuthorize("@ss.hasPermi('engineering:change:list')")
    @GetMapping("/list")
    public TableDataInfo list(ChangeOrderMaster model) {
        startPage();
        // createBy 设置成当前登录用户，根据用户标段查询数据，排除admin
        if (!SecurityUtils.isAdmin(getUserId())) {
            model.setCreateBy(getUsername());
        }
        List<ChangeOrderMaster> list = service.selectModelList(model);
        return getDataTable(list);
    }

    /**
     * 获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('engineering:change:query')")
    @GetMapping(value = "/{changeId}")
    public AjaxResult getInfo(@PathVariable("changeId") Long changeId) {
        return AjaxResult.success(service.getModelById(changeId));
    }

    /**
     * 新增
     */
    @PreAuthorize("@ss.hasPermi('engineering:change:add')")
    @Log(title = "变更令申报", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ChangeOrderMaster model) {
        // 校验变更令号是否重复
        if (UserConstants.NOT_UNIQUE.equals(service.checkChangeOrderNoUnique(model))) {
            return AjaxResult.error("新增变更令申报'" + model.getChangeOrderNo() + "'失败，变更令号已存在");
        }
        // 如果本期已经汇总批复，将无法进行工程计量
        ProPeriod period = periodService.getPeriodByPeriodName(model.getInstitutionCode(), model.getPeriodName());
        if (Constants.YES.equals(period.getIsLock())) {
            return AjaxResult.error("本期已批复汇总，无法进行变更令申报操作");
        }
        return toAjax(service.insert(model));
    }

    /**
     * 修改
     */
    @PreAuthorize("@ss.hasPermi('engineering:change:edit')")
    @Log(title = "变更令申报", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ChangeOrderMaster model) {
        // 校验变更令号是否重复
        if (UserConstants.NOT_UNIQUE.equals(service.checkChangeOrderNoUnique(model))) {
            return AjaxResult.error("修改变更令申报'" + model.getChangeOrderNo() + "'失败，变更令号已存在");
        }
        return toAjax(service.update(model));
    }

    /**
     * 删除
     */
    @PreAuthorize("@ss.hasPermi('engineering:change:remove')")
    @Log(title = "变更令申报", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(service.deleteByIds(ids));
    }

    /**
     * 传送
     *
     * @param delivery 传送对象
     * @return 结果
     */
    @PreAuthorize("@ss.hasPermi('engineering:change:delivery')")
    @Log(title = "变更令申报", businessType = BusinessType.DELIVERY)
    @PostMapping("/delivery")
    public AjaxResult delivery(@RequestBody WorkflowTaskDelivery delivery) {
        return toAjax(service.delivery(delivery));
    }

    /**
     * 锁定
     *
     * @param ids 主键集合
     * @return 结果
     */
    @PreAuthorize("@ss.hasPermi('engineering:change:lock')")
    @Log(title = "变更令申报", businessType = BusinessType.LOCK)
    @GetMapping("/lock/{ids}")
    public AjaxResult lock(@PathVariable Long[] ids) {
        return toAjax(service.lock(ids));
    }

    /**
     * 解锁
     *
     * @param changeId 主键
     * @return 结果
     */
    @PreAuthorize("@ss.hasPermi('engineering:meterage:unlock')")
    @Log(title = "变更令申报", businessType = BusinessType.UNLOCK)
    @GetMapping(value = "/unlock/{changeId}")
    public AjaxResult unlock(@PathVariable("changeId") Long changeId) {
        ChangeOrderMaster master = service.getModelById(changeId);
        // 如果该变更令号下的计量清单已经做过计量，将无法进行解锁操作
        if (boqMeterageService.hasPartakeMeterageByChangeOrderNo(master.getChangeOrderNo())) {
            return error("该变更令号下的变更计量清单已参与计量，不能进行解锁操作");
        }
        // 查看本期是否最终批复汇总，如果最终批复汇总，将无法进行解锁操作
        if (summaryService.getSummaryIsApprove(master.getInstitutionCode(), master.getPeriodName())) {
            return error("本期数据已最终批复汇总，请先进行未批复汇总后再进行解锁操作");
        }
        return toAjax(service.unlock(master, changeId));
    }

}
